﻿@*
    // Copyright (c) Alex Reich.
    // Licensed under the CC BY 4.0 License.
*@
@using RulesEngineEditor.Models
@inject RulesEngineEditor.Services.WorkflowService WorkflowService

@if (scopedParams != null)
{
    if (scopedParams.Count > 0)
    {
        <button class="reeditor_button @IsPressed(sort, "button_depressed")" @onclick="(() => sort = !sort)">Arrange</button>
        <span>&nbsp;</span>
        <sp_grid_kvp>
            <div>Scoped Param Name</div>
            <div>Expression</div>
        </sp_grid_kvp>
    }
    @if (sort)
    {
        <Dropzone TItem="ScopedParamData" Items="scopedParams" OnItemDrop="@((i)=>ScopedParamDragEnd(i))">
            <div class="rules_arrange">
                <ScopedParamEditor scopedParam="@context" ScopedParamDelete="DeleteScopedParam" />
            </div>
        </Dropzone>
    }
    else
    {
        @foreach (var context in scopedParams)
        {
            <ScopedParamEditor scopedParam="@context" ScopedParamDelete="DeleteScopedParam" />
        }
    }
}
@code {
    [Parameter]
    public List<ScopedParamData> scopedParams { get; set; }

    [Parameter]
    public EventCallback<List<ScopedParamData>> ScopedParamsChanged { get; set; }

    public override async Task SetParametersAsync(ParameterView parameters)
    {
        await base.SetParametersAsync(parameters);
    }

    private void DeleteScopedParam(ScopedParamData sp)
    {
        scopedParams.Remove(sp);
        ScopedParamsChanged.InvokeAsync(scopedParams);
    }

    private void ScopedParamDragEnd(ScopedParamData sp)
    {
        WorkflowService.Sort(scopedParams);
        ScopedParamsChanged.InvokeAsync(scopedParams);
    }

    public bool sort;

    public string IsPressed(bool state, string suffix)
    {
        return state ? "reeditor_" + suffix : "";
    }
}
